// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import BaseUI from "../framwork/BaseUI";
import qxc_Node from "../framwork/qxcTools/qxc_node";
import Game from "./Game";
import gridItem from "./gridItem";
import grids from "./grids";
import landGridItem from "./landGridItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class newLandItem extends BaseUI implements qxcUI{
    @property(cc.Node)
    newLands:cc.Node = null;
    baseUI_onLoad(): void {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart,this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove,this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd,this)
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel,this)
    }
    touchStart(event){
        this.node.parent = cc.director.getScene();
        this.node.setPosition(event.getLocation());
        console.log("this.touchStart");
    }
    touchMove(event){
        this.node.setPosition(event.getLocation());
    }
    touchEnd(event){
        let touchGrids:gridItem[] = this.getAllTouchGrids();
        console.log(touchGrids);
        if(touchGrids){
            this.setAllTouchGrids();
        } else {
            this.goBack();
        }
    }
    getAllTouchGrids():gridItem[]{//获取触摸到的所有格子
        let moveGrids:landGridItem[] = this.getComponentsInChildren(landGridItem);
        let touchGrids:gridItem[] = [];
        for (let index = 0; index < moveGrids.length; index++) {
            let touchGrid:gridItem = this.getTouchGrid(moveGrids[index]);
            if(touchGrid && touchGrid.data == null){
                touchGrids.push(touchGrid);
            } else {
                return null;
            }
        }
        return touchGrids;
    }
    setAllTouchGrids(){//设置所有触摸的格子
        let moveGrids:landGridItem[] = this.getComponentsInChildren(landGridItem);
        for (let index = 0; index < moveGrids.length; index++) {
            let touchGrid:gridItem = this.getTouchGrid(moveGrids[index]);
            touchGrid.setData(moveGrids[index].data);
        }
        Game.getInstance().createNewData();
        Game.getInstance().createNewLand();
        this.node.destroy();
    }
    getTouchGrid(moveGrid:gridItem){//获取触摸到的格子
        let list = grids.getInstance().getAllGridItems();
        for (let index = 0; index < list.length; index++) {
            const element = qxc_Node.getNodeToNodeLength(list[index].node,moveGrid.node);
            if(element < 65){
                return list[index]
            }
        }
        return null;
    }
    touchCancel(event){
        this.goBack();
    }
    goBack(){
        this.node.x = 0;
        this.node.parent = this.newLands;
    }
    baseUI_onDestroy(): void {

    }
    onBtn_click(target: cc.Button, onBtn_name: string): number | void {
        switch (onBtn_name) {
            case "btn_shun":
                this.node.angle -= 90;
                this.GetGameObject("btn_shun").angle += 90;
                this.GetGameObject("btn_ni").angle += 90;
                let moveGrids1:landGridItem[] = this.getComponentsInChildren(landGridItem);
                for (let index = 0; index < moveGrids1.length; index++) {
                    moveGrids1[index].node.angle += 90;
                }
                break;
            case "btn_ni":
                this.node.angle += 90;
                this.GetGameObject("btn_shun").angle -= 90;
                this.GetGameObject("btn_ni").angle -= 90;
                let moveGrids2:landGridItem[] = this.getComponentsInChildren(landGridItem);
                for (let index = 0; index < moveGrids2.length; index++) {
                    moveGrids2[index].node.angle -= 90;
                }
                break;
        }
    }
    init_data(d: any): void {   

    }
    setData(data){
        let list:landGridItem[] = this.getComponentsInChildren(landGridItem);
        for (let index = 0; index < list.length; index++) {
            list[index].setData(data[index]);
        }
    }
}
